Working with Character Entities

Some characters are reserved in HTML and we cannot use them as a regular character in the HTML, code. For example, it is not possible to use the less than (<) sign, as the browser will mix them with tags. Therefore, if you want the browser to display the reserved characters, then they must be replaced with character entities in the HTML code. A character entity has three parts: an ampersand (&), an entity name or # and an entity number, and a semicolon (;).

For example, if you want to insert the symbol of copyright on a Web page, then you need to provide its entity name as &copy or entity number as &#169. Below given table briefly describes commonly used character entities:

Entity Name

Description

Result

Entity Number

&nbsp;

Non-breaking space

 

&#160;

&lt;

Less than

<

&#60

&gt;

Greater than

>

&#62;

&amp;

Ampersand

&

&#38;

&quot;

Quotation mark

&#34;

&iexcl;

Inverted Exclamation Mark

!

&#161;

Let’s do the following steps to work with character entities:


<!DOCTYPE html>
<head>
<title>Character Entities</title>
</head>
<body>
    <h4>Using Character Entities</h4>
    <font size=”4”>
    This &nbsp; &nbsp; Is &nbsp; &nbsp; an &nbsp; &nbsp; example &nbsp; &nbsp; of &nbsp; &nbsp; &nbsp; inserting &nbsp; &nbsp; &nbsp; spaces.
    <p>This is the &lt; (less than) sign.</p>
    <p>This is the &gt; (greater than) sign.</p>
    <p>This is the &amp; (ampersand) sign.</p>
    <p>This is the &quot; (quotation) mark.</p>
    <p>This is the &lexcle; (Inverted Exclamation) mark.</p>
    </font>
</body>
</html>

Save the document with the name CharacterEntities.html.

Note: Character entities are case-sensitive; therefore, they are always given in lower case.

Commenting the Text

A comment tag is used to hide the text from the Web page. Comments are written inside the opening and closing (<!..and ..>) tags, which hide any content between them from the Web browser. In this way, you can place code-specific information inside the comment tags. Comments are not visible to the user when the program is executed, but they are still available in the program.

Let’s do the following steps to comment the text:


<!DOCTYPE html>
<head>
<title>Commenting text</title>
</head>
<body>
    <!--This is a comment, --->
    <!—This comment will not be displayed
    <p> This text will be displayed.
    This is a normal text.
    </p>
</body>
</html> 

Save the document with the name CommentingText.html.